<template>
  <div class="course_item" @click="goDetails">
    <div class="course_img">
      <el-image
        fit="cover"
        :src="props.item.fengmiantu"
        :lazy="true"
      />
    </div>
    <div class="course_text">
      <div class="course_title">
        {{ props.item.kechengbiaoti }}
      </div>
      <div class="course_tip">
        {{ props.item.kechengjianjie }}
      </div>
      <div class="course_price">
        <div class="price_1" v-if="props.item.shoujia == 0">免费</div>
        <div class="price_2" v-else>¥{{ props.item.shoujia }}</div>
        <div class="course_name">{{props.item.xunixuexirenshu}}人已学习</div>
      </div>
    </div>
  </div>
</template>

<script setup name="courseItem">
import { ref, reactive, inject } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
import { storeToRefs } from 'pinia'
import { useUserStore } from "@/stores/user";
let userStore = useUserStore();
let { token } = storeToRefs(userStore);

const injectMessage = inject("$message");
const props = defineProps({
    item: {
        type: Object,
        default: () => {},
    },
});

function goDetails() {
  if(!token.value) {
    injectMessage.error('请登录');
    router.push({
      path: '/login'
    })
  } else  {
    router.push({
        path: "/details",
        query: {
            aid: props.item.aid,
        }
    })
  }
    
}
</script>


<style lang="scss" scoped>
.course_item {
  float: left;
  width: 168px;
  height: 171px;
  background: #fff;
  margin: 0 16px 16px 0;
  cursor: pointer;
  .course_img {
    width: 168px;
    height: 90px;
  }
  .course_text {
    padding: 4px 7px 0;
    .course_title {
      height: 28px;
      font-size: 9px;
      font-weight: 500;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 2;
    }
    .course_tip {
      font-size: 8px;
      color: #666;
      margin-top: 5px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 1;
    }
    .course_price {
      margin-top: 6px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .price_1 {
        color: #62ce96;
        font-size: 10px;
        font-weight: bold;
      }
      .price_2 {
        color: #f43024;
        font-size: 10px;
        font-weight: bold;
      }
      .course_name {
        font-size: 8px;
        color: #999;
      }
    }
  }
}
.course_item:nth-child(4n + 4) {
    margin: 0 0 16px 0;
}
</style>